package com.xt.purchasingsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.purchasingsystem.annotation.PurchaseCache;
import com.xt.purchasingsystem.annotation.PurchaseClearCache;
import com.xt.purchasingsystem.dto.SysRolesDto;
import com.xt.purchasingsystem.model.SysRoles;
import com.xt.purchasingsystem.service.SysRolesService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/role")
@Api(description = "角色管理API")
public class SysRolesController {
    @Autowired
    private SysRolesService sysRolesService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有角色的接口
     *
     * @return
     */
    @ApiOperation(value = "角色全部列表", notes = "角色全部列表")
    @GetMapping("/queryAllRoles")
    @PurchaseCache(prefix = "role:roles:", timeout = 7200, random = 1000)
    public Result queryAllRoles() {
        List<SysRoles> sysRoles = sysRolesService.list(null);
        return ResultFactory.buildSuccessResult("查询成功", sysRoles);
    }

    /**
     * 多条件分页查询角色的接口
     *
     * @param sysRolesDto
     * @return
     */
    @ApiOperation(value = "多条件分页查询角色列表",notes = "多条件分页查询角色列表")
    @PostMapping("/querySysRolesBySysRolesDto")
    @PurchaseCache(prefix = "role:roles:roleDto:", timeout = 7200, random = 1000)
    public Result querySysRolesBySysRolesDto(@RequestBody @ApiParam(name = "sysRolesDto", value = "角色查询条件", required = true) SysRolesDto sysRolesDto) {
        QueryWrapper<SysRoles> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysRolesDto.getSysRoles().getName())) {
            queryWrapper.like("name", sysRolesDto.getSysRoles().getName());
        }
        IPage<SysRoles> page = sysRolesService.page(new Page<>(sysRolesDto.getCurrNo(), sysRolesDto.getPageSize()), queryWrapper);
        return ResultFactory.buildSuccessResult("查询成功",page);

    }

    /**
     * 根据角色名查询角色的接口 (用于注册角色时检查角色名是否已被注册)
     *
     * @param roleName 角色名
     * @return
     */
    @ApiOperation(value = "根据角色名查询角色的接口",notes = "根据角色名查询角色的接口")
    @ApiImplicitParam(paramType = "path", name = "roleName", value = "角色名称", required = true, dataType = "String")
    @GetMapping("/checkRoleName/{roleName}")
    @PurchaseCache(prefix = "role:name:", timeout = 7200, random = 1000)
    public Result checkRoleName(@PathVariable("roleName") String roleName) {
        SysRoles sysRoles = sysRolesService.getOne(new QueryWrapper<SysRoles>().eq("name", roleName));
        if (sysRoles != null) {
            return ResultFactory.buildSuccessResult("查询成功");
        } else {
            return ResultFactory.buildFailResult("查询失败");

        }
    }

    /**
     * 添加或修改角色的接口
     *
     * @param sysRoles 角色对象
     * @return
     */
    @ApiOperation(value = "添加或修改角色的接口",notes = "添加或修改角色的接口")
    @PostMapping("/saveOrUpdateRole")
    @PurchaseClearCache(prefix = "role")
    public Result saveOrUpdate(@RequestBody @ApiParam(name = "sysRoles", value = "角色对象", required = true) SysRoles sysRoles) {
        if (StringUtils.isEmpty(sysRoles.getId())) {
            sysRoles.setId(String.valueOf(idWorker.nextId()));
            boolean flag = sysRolesService.saveOrUpdate(sysRoles);
            if (flag) {
                return ResultFactory.buildSuccessResult("添加成功");
            } else {
                return ResultFactory.buildFailResult("添加失败");
            }

        } else {
            boolean flag = sysRolesService.saveOrUpdate(sysRoles);
            if (flag) {
                return ResultFactory.buildSuccessResult("修改成功");
            } else {
                return ResultFactory.buildFailResult("修改失败");
            }
        }

    }

    /**
     * 根据角色编号集合删除对应角色的接口
     *
     * @param
     * @return
     */
    @ApiOperation(value = "根据角色编号集合删除对应角色的接口")

    @DeleteMapping("/deleteRoleById")
    @PurchaseClearCache(prefix = "role")
    public Result deleteRoleById(@RequestBody @ApiParam(name = "sysRolesDto", value = "角色Dto对象", required = true) SysRolesDto sysRolesDto) {
        boolean flag = sysRolesService.removeByIds(sysRolesDto.getRoleIds());
        if (flag) {
            return ResultFactory.buildSuccessResult("删除成功");
        } else {
            return ResultFactory.buildFailResult("删除失败");
        }
    }

    /**
     * 给角色授权菜单的接口
     *
     * @return
     */
    @ApiOperation(value = "给角色授权菜单的接口",notes = "给角色授权菜单的接口")

    @PutMapping("/authorizationMenusByRole")
    @PurchaseClearCache(prefix = {"role:menusByRole:","menu:menusBy"})
    public Result authorizationMenusByRole(@RequestBody @ApiParam(name = "sysRolesDto", value = "角色Dto对象", required = true) SysRolesDto sysRolesDto) {
        return sysRolesService.authorizationMenusByRole(sysRolesDto);
    }

    /**
     * 根据角色编号查询此角色对应菜单的接口
     *
     * @param roleId 角色编号
     * @return
     */
    @ApiOperation(value = "根据角色编号查询此角色对应菜单的接口")
    @ApiImplicitParam(paramType = "path", name = "roleId", value = "角色编号", required = true, dataType = "Long")
    @PurchaseCache(prefix = "role:menusByRole:", timeout = 7200, random = 1000)
    @GetMapping("/getMenusByRole/{roleId}")
    public Result getMenusByRole(@PathVariable("roleId") Long roleId) {
        return sysRolesService.getAllMenusByRole(roleId);
    }
}

